﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO_HealthInsurance;
using BLL_HealthInsurance;
using System.Data;
namespace HealthInsuranceMgm
{
    public partial class EmployeeDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            load();
            PanelUpdateEmp.Visible = false;
            PanelListEmp.Visible = true;
        }
        public void load()
        {
            BLLEmployee bll = new BLLEmployee();
            grvEmp.DataSource = bll.ListEmployee();
            grvEmp.DataBind();
        }
        public void LoadData(int empno)
        {
         
            BLLEmployee obj = new BLLEmployee();
            DTOEmployee objDt = new DTOEmployee();
            objDt.EmpNo = empno;
            DataTable dt = obj.GetEmployeeById(objDt);
            HiddenField1.Value = dt.Rows[0]["EmpNo"].ToString();
            txtdesignation.Text = dt.Rows[0]["Designation"].ToString();
            txtjoindate.Text = dt.Rows[0]["Joindate"].ToString();
            txtsalary.Text = dt.Rows[0]["Salary"].ToString();
            txtfirstname.Text = dt.Rows[0]["FirstName"].ToString();

            txtlastname.Text = dt.Rows[0]["LastName"].ToString();
            txtusername.Text = dt.Rows[0]["UserName"].ToString();
            txtpassword.Text = dt.Rows[0]["PassWord"].ToString();
            txtaddress.Text = dt.Rows[0]["Address"].ToString();

            txtcontact.Text = dt.Rows[0]["ContactNo"].ToString();
            txtstate.Text = dt.Rows[0]["State"].ToString();
            txtcoutry.Text = dt.Rows[0]["Country"].ToString();
            txtcity.Text = dt.Rows[0]["City"].ToString();

           

        }
        protected void grvEmp_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            int EmpNo = Int32.Parse(grvEmp.DataKeys[e.NewSelectedIndex].Value.ToString());
            LoadData(EmpNo);


            PanelListEmp.Visible = false;
            PanelUpdateEmp.Visible = true;
        }
        public SortDirection SortDirection
        {
            get
            {

                if (ViewState["SortDirection"] == null)
                {
                    ViewState["SortDirection"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["SortDirection"];
            }
            set
            {
                ViewState["SortDirection"] = value;
            }
        }
        protected void grvEmp_Sorting(object sender, GridViewSortEventArgs e)
        {
            BLLEmployee obj = new BLLEmployee();
            string sortExpression = e.SortExpression;
            string Direction = "";

            if (SortDirection == SortDirection.Ascending)
            {
                SortDirection = SortDirection.Descending;
                Direction = "DESC";
            }
            else
            {
                SortDirection = SortDirection.Ascending;
                Direction = "ASC";
            }
            DataTable dt = obj.ListEmployee();
            dt.DefaultView.Sort = sortExpression + " " + Direction;
            grvEmp.DataSource = dt;
            grvEmp.DataBind();
        }

      

        protected void grvEmp_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Empno = (int)grvEmp.DataKeys[e.RowIndex].Value;
            BLLEmployee obj = new BLLEmployee();
            DTOEmployee g = new DTOEmployee();
            g.EmpNo = Empno;
            obj.deleteEmployee(g);
            load();
        }

        protected void grvEmp_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
                l.Attributes.Add("onclick", "javascript: return " +
                "confirm('Are you sure you want to delete this record " +
                DataBinder.Eval(e.Row.DataItem, "EmpNo") + "')");

            }
        }

        protected void grvEmp_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            BLLEmployee obj = new BLLEmployee();
            DTOEmployee objDt = new DTOEmployee();
            if (e.CommandName == "Delete")
            {
                // get the categoryID of the clicked row
                int empno = Convert.ToInt32(e.CommandArgument);
                // Delete the record 
                objDt.EmpNo = empno;
                obj.deleteEmployee(objDt);
                // Implement this on your own :) 
            }
        }

        protected void grvEmp_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvEmp.PageIndex = e.NewPageIndex;
            load();
        }
        public void message(string script)
        {

            Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", script, true);
        }
        protected void btnupdate_Click(object sender, EventArgs e)
        {
            int empNo = Int32.Parse(HiddenField1.Value.ToString());
            string designation = txtdesignation.Text;
            string joindate = txtjoindate.Text;
            float salary = float.Parse(txtsalary.Text);
            string firstName = txtfirstname.Text;
            string lastname = txtlastname.Text;
            string userName = txtusername.Text;
            string password = txtpassword.Text;
            string address = txtaddress.Text;
            string contactNo = txtcontact.Text;
            string state = txtstate.Text;
            string Country = txtcoutry.Text;
            string city = txtcity.Text;
            if(IsValid){
                DTOEmployee objdto = new DTOEmployee();
                BLLEmployee objbll = new BLLEmployee();
                objdto.EmpAddress = address;
                objdto.EmpCity = city;
                objdto.EmpContactNo = contactNo;
                objdto.EmpCountry = Country;
                objdto.EmpDesignation = designation;
                objdto.EmpFirstName = firstName;
                objdto.EmpLastName = lastname;
                objdto.EmpNo = empNo;
                objdto.EmpPassword = password;
                objdto.EmpSalary = salary;
                objdto.EmpState = state;
                objdto.EmpUserName = userName;
                objdto.EmpJoindate = joindate;

              int i =  objbll.updateEmployee(objdto);
              load();

                if(i>0){
                    message("alert('Update Successfull');");
                    
                }

            
            }

        }
    }
}